<?php
namespace app\first\controller;

use app\first\model\Filter;
use app\first\model\Message;
/**
 * Class Article
 * @title 帖子相关
 * @url /v1/article
 * @desc  帖子相关
 * @version 1.0
 * @package app\first\article
 * @route('v1/article')
 */
class Article extends Base{
    //是否开启授权认证,默认开启
    public $apiAuth = true;
    protected $model = null;
    protected $validate = null;
    /*
    * 需要附加的其他方法,比如:topList,newList
    * protected $extraActionList = ['topList','newList',...];
    * 已经对核心的类进行了重写,扩展的新方法会自动添加到认证中,无需再手动书写
    */
    protected $extraActionList = [];

    public function __construct(){
        parent::__construct();
        $this->model = new \app\first\model\Article();
        $this->validate = new \app\first\validate\Article();
    }

    /**
     * @title 获取帖子列表
     * @method index
     * @param int $limit 每页条数 true 10
     * @param int $page 当前页数 true 1
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function index($limit=10,$page = 1){
        $filter = new Filter();
        $ids = $filter->getFilterIds(self::$uid);
        if($ids){
            $map[] = ['mid','not in',$ids];
        }else{
            $map = [];
        }
        $limit = input('limit',10);
        $order="create_time desc";
        $list = $this->model->getList($map,$order,$limit);
        return $this->sendSuccess($list);
    }

    /**
     * @title 获取帖子详情
     * @method read
     * @param int $id 帖子id true
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function read($id=0){
        $info = $this->model->getOneById($id);
        $info->hidden(['update_time','sort']);
        return $this->sendSuccess($info);
    }

    /**
     * @title 删除
     * @method delete
     * @param int $id 资源id true
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \Exception
     */
    public function delete($id = 0){
        $info = $this->model->get($id);
        if(!$info->delete()){
            return $this->sendError(0,'删除失败');
        }
        return $this->sendSuccess('','删除成功');
    }

    /**
     * @title 帖子评论
     * @method message
     * @param int $aid 帖子id true
     * @param int $limit 每页数量 true 10
     * @param int $page 当前页 true 1
     * @route('v1/article/message','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function message($aid=0,$limit=10,$page=1){
        $message = new Message();
        $map[]=['aid','eq',$aid];
        $result = $message->getList($map,'create_time desc',$limit);
        if(!$result){
            $result = [];
        }
        return $this->sendSuccess($result);
    }
}